<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/8/12 0012
 * Time: 20:01
 */

namespace api\controllers;


use api\components\BaseApiComponentController;
use common\components\DataHelper;
use common\models\AgentInfo\AgentInfo;
use common\models\AgentPlugs\AgentPlugs;
use common\models\Plug\Plug;
use common\models\PlugsRunDesc\PlugsRunDesc;
use common\models\PlugsRunTotal\PlugsRunTotal;

class PlugsRunController extends BaseApiComponentController
{
    public function actionUpload()
    {
        $date = date("Y-m-d");
        $mac = $this->requestAll("mac");
        $plugsRunDesc = PlugsRunDesc::findOne([
            "created_date" => $date,
            "mac" => $mac
        ]);
        if ( !$plugsRunDesc ) {
            $plugsRunDesc = new PlugsRunDesc();
            $plugsRunDesc->created_date = $date;
            $plugsRunDesc->updated_time = $date;
            $plugsRunDesc->mac = $mac;
            $plugsRunDesc->is_collect = PlugsRunDesc::IS_NO;
          //  $plugsRunDesc->type = PlugsRunDesc::TYPE_NO;
        } else {
            $transaction = PlugsRunDesc::getDb()->beginTransaction();
            $plugsRunDesc->lockForUpdate();
        }
        $plugsRunDesc->attributes = $this->post();
        $plugsRunDesc->ip = DataHelper::getIp();
        if ( !$plugsRunDesc->agent_id ) {
            $plugsRunDesc->agent_id = AgentInfo::getDefAgentUid();
        }

        /*if ( $plugsRunDesc->getIsNewRecord() || $plugsRunDesc->isAttributeChanged("adid") ) {
            //验证插件id
            $plug = Plug::findOne($plugsRunDesc->adid);
            if ( !$plug ) {
                isset($transaction) && $transaction->rollBack();
                return $this->jsonResponse(self::RESPONSE_FAILURE, "无效的插件id");
            }
            if ( $plug->status != Plug::STATUS_YES ) {
                isset($transaction) && $transaction->rollBack();
                return $this->jsonResponse(self::RESPONSE_FAILURE, "该插件已被禁用");
            }

        }*/

        //校验代理商 设置区域经理
        if ( $plugsRunDesc->getIsNewRecord() || $plugsRunDesc->isAttributeChanged("agent_id") ) {
            $agentInfo = AgentInfo::findOne([
                "uid" => $plugsRunDesc->agent_id
            ]);
            if ( !$agentInfo ) {
                $plugsRunDesc->manager_uid = 0;
            } else {
                $plugsRunDesc->manager_uid = $agentInfo->parent_manager;
            }
        }

        /*if ( $plugsRunDesc->isAttributeChanged("adid") || $plugsRunDesc->isAttributeChanged("agent_id") ) {
            $ret = AgentPlugs::findOne([
                "agent_uid" => $plugsRunDesc->agent_id,
                "plug_id" => $plugsRunDesc->adid
            ]);
            if ( !$ret ) {
                isset($transaction) && $transaction->rollBack();
                return $this->jsonResponse(self::RESPONSE_FAILURE, "业务与代理商不对应");
            }
        }*/

        if ( $plugsRunDesc->save() ) {
            //没有参与汇总并且有效
            if ( !$plugsRunDesc->isCollect() ) {
                if ( !PlugsRunTotal::writeTotal($plugsRunDesc) ) {
                    isset($transaction) && $transaction->rollBack();
                    return $this->jsonResponse(self::RESPONSE_SUCCESS, PlugsRunTotal::error());
                }
            }
            isset($transaction) && $transaction->commit();
            return $this->jsonResponse(self::RESPONSE_SUCCESS, "操作成功");
        }
        isset($transaction) && $transaction->rollBack();
        return $this->jsonResponse(self::RESPONSE_FAILURE, $plugsRunDesc->getFirstErrorToStr(), $plugsRunDesc->getFirstErrors());
    }
}